package com.aiyige.page.my.customer.dialog;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.page.my.customer.adapter.DialogBottomCheckboxAdapter;
import com.aiyige.page.my.customer.model.CheckboxSelectionEntity;
import com.aiyige.utils.widget.BaseBottomDialogFragment;
import com.aiyige.utils.widget.autoLayoutManager.AutoLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xiachao on 2018/4/10.
 */

public class CheckboxBottomDialog extends BaseBottomDialogFragment {

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;

    @BindView(R.id.tv_title)
    TextView tvTitle;


    Unbinder unbinder;

    DialogBottomCheckboxAdapter adapter;
    List<CheckboxSelectionEntity> list;

    public static CheckboxBottomDialog newInstance() {
        return new CheckboxBottomDialog();
    }

    Listener listener;
    String titleName;

    public void setLisenter(Listener callback) {
        this.listener = callback;
    }

    public void setData(List<CheckboxSelectionEntity> list) {
        this.list = list;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_bottom_checkbox, container, false);
        unbinder = ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        adapter = new DialogBottomCheckboxAdapter();
        AutoLayoutManager layout = new AutoLayoutManager();
        //必须，防止recyclerview高度为wrap时测量item高度0
        layout.setAutoMeasureEnabled(true);
        recyclerView.setLayoutManager(layout);
        recyclerView.setAdapter(adapter);
        adapter.setNewData(list);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
//                CheckboxBottomDialog.this.adapter.reset();
                list.get(position).setSelected(!list.get(position).isSelected());
                CheckboxBottomDialog.this.adapter.notifyDataSetChanged();
            }
        });
        tvTitle.setText(titleName);
    }

    public void setTitle(String titleName) {
        this.titleName = titleName;
    }



    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private List<CheckboxSelectionEntity> getSelected() {
        List<CheckboxSelectionEntity> selectedList = new ArrayList<>();
        for (CheckboxSelectionEntity entity : list) {
            if (entity.isSelected()) {
                selectedList.add(entity);
            }
        }
        return selectedList;
    }

    private void doFinish() {
        if (listener != null) {
            listener.onSelect(getSelected());
        }
        dismiss();
    }

    @OnClick({R.id.btn_cancel, R.id.tv_ok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.tv_ok:
                doFinish();
                break;
        }
    }

    public interface Listener {
        void onSelect(List<CheckboxSelectionEntity> selectedList);
    }
}
